package cn.samler.diandi.api.service.impl;

import cn.samler.diandi.api.entity.Post;
import cn.samler.diandi.api.entity.PostComment;
import cn.samler.diandi.api.entity.User;
import cn.samler.diandi.api.mapper.PostCommentMapper;
import cn.samler.diandi.api.mapper.PostMapper;
import cn.samler.diandi.api.model.io.Response;
import cn.samler.diandi.api.model.io.request.post.*;
import cn.samler.diandi.api.model.io.request.pub.Page;
import cn.samler.diandi.api.service.PostService;
import cn.samler.diandi.api.utils.ContextHolderUtil;
import cn.samler.diandi.api.utils.ResponseBuilder;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    private final PostMapper postMapper;
    private final PostCommentMapper postCommentMapper;

    public PostServiceImpl(PostMapper postMapper, PostCommentMapper postCommentMapper) {
        this.postMapper = postMapper;
        this.postCommentMapper = postCommentMapper;
    }

    /**
     * 列出自己的post列表
     * @param form page表单
     * @return Response
     */
    @Override
    public Response list(Page form) {
        User user = (User) ContextHolderUtil.getCurrentUser();
        List<Post> list = postMapper.queryByPage(user.getId(), form.getStart(), form.getOffset());

        return new ResponseBuilder().success().data(list).build();
    }

    /**
     * 列出附近需求
     *
     * @param form role and range
     * @return Response
     */
    @Override
    public Response nearby(Nearby form) {
        List<SearchResult> list = postMapper.nearby(form);

        return new ResponseBuilder().success().msg("获取成功").data(list).build();
    }

    /**
     * 搜索附近有效的post
     * @param form 搜索表单
     * @return Response
     */
    @Override
    public Response search(Search form) {
        List<SearchResult> list = postMapper.search(form);

        return new ResponseBuilder().success().data(list).build();
    }

    /**
     * 通过id查找对应的post
     * @param id post id
     * @return Response
     */
    @Override
    public Response findById(Integer id) {
        SearchResult post = postMapper.findById(id);
        User user = (User) ContextHolderUtil.getCurrentUser();

        // 下架的post对他人不可见
        if (post != null && !post.getUid().equals(user.getId()) && !post.getDisplay()) {
            post = null;
        }

        return new ResponseBuilder().success().data(post).build();
    }

    /**
     * 发布新的post
     * @param form 数据表单
     * @return Response
     */
    @Override
    public Response publish(Publish form) {
        User user = (User) ContextHolderUtil.getCurrentUser();

        // 表单实体转换到entity
        Gson gs = new Gson();
        String jsonStr = gs.toJson(form);
        Post post = gs.fromJson(jsonStr, Post.class);

        // 绑定发布者uid
        post.setUid(user.getId());

        Integer result = postMapper.insert(post);
        if(result > 0)
           return new ResponseBuilder().success().msg("发布成功").build();

        return new ResponseBuilder().failure().msg("发布失败").build();
    }

    /**
     * 更新post
     * @param form 数据表单
     * @return Response
     */
    @Override
    public Response update(Post form) {
        User user = (User) ContextHolderUtil.getCurrentUser();
        form.setUid(user.getId());

        if(postMapper.update(form) > 0)
            return new ResponseBuilder().success().data("更新成功").build();

        return new ResponseBuilder().failure().msg("更新失败").build();
    }

    /**
     * 删除一个post
     * @param id post id
     * @return Response
     */
    @Override
    public Response remove(Integer id) {
        User user = (User) ContextHolderUtil.getCurrentUser();

        if(postMapper.delete(user.getId(), id) > 0)
            return new ResponseBuilder().success().msg("删除成功").build();

        return new ResponseBuilder().failure().msg("删除失败").build();
    }

    /**
     * 改变一个post的显示状态
     * @param id post id
     * @param display 是否显示
     * @return Response
     */
    @Override
    public Response display(Integer id, boolean display) {
        User user = (User) ContextHolderUtil.getCurrentUser();
        Post post = new Post();

        post.setId(id);
        post.setUid(user.getId());
        post.setDisplay(display);

        if(postMapper.update(post) > 0)
            return new ResponseBuilder().success().msg("操作成功").build();

        return new ResponseBuilder().failure().msg("操作失败").build();
    }

    /**
     * 获取评论列表
     *
     * @param form 分页表单
     * @return Response
     */
    @Override
    public Response listComment(PostCommentList form) {
        Page page = form.getPage();
        List<PostComment> list = postCommentMapper.listByPostId(form.getPid(), page.getStart(), page.getOffset());

        if(list != null)
            return new ResponseBuilder().success().msg("获取成功").data(list).build();

        return new ResponseBuilder().failure().msg("获取失败").build();
    }

    /**
     * 发表评论/回复
     *
     * @param form 数据表单
     * @return Response
     */
    @Override
    public Response publishComment(PostComment form) {
        User user = (User) ContextHolderUtil.getCurrentUser();
        form.setUid(user.getId());

        // 这里判断是否提交了uid，如果没有提交正确的uid就重置回复状态，避免恶意提交
        if(form.getReplyUid() != null && form.getReplyUid() > 0)
            form.setIsReply(true);
        else {
            form.setIsReply(false);
            form.setReplyUid(null);
        }

        if(postCommentMapper.insert(form) > 0)
            return new ResponseBuilder().success().msg("发表成功").build();

        return new ResponseBuilder().failure().msg("发表失败").build();
    }

}
